package com.zhida.chatservice.config;


import com.zhida.chatservice.handler.DefaultWebSocketHandler;
import com.zhida.chatservice.interceptor.WebSocketInterceptor;
import com.zhida.chatservice.service.WebSocket;
import com.zhida.chatservice.service.impl.WebSocketImpl;
import lombok.NonNull;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;




/**
 * Create by zhh 2024/7/8 20:54
 */
@Configuration
@EnableWebSocket
public class WebSocketConfiguration implements WebSocketConfigurer {

    @Bean
    public DefaultWebSocketHandler defaultWebSocketHandler() {
        return new DefaultWebSocketHandler();
    }

    @Bean
    public WebSocket webSocket() {
        return new WebSocketImpl();
    }

    @Bean
    public WebSocketInterceptor webSocketInterceptor() {
        return new WebSocketInterceptor();
    }

    @Override
    public void registerWebSocketHandlers(@NonNull WebSocketHandlerRegistry registry) {
        System.out.println("启用WebSocketConfiguration");
        registry.addHandler(defaultWebSocketHandler(), "ws/connect")
                .addInterceptors(webSocketInterceptor())
                .setAllowedOrigins("*");
    }
}

